package com.source.barcodeMachineStatus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineGrantAndRecovery.entity.HouseMachine;
import com.source.barcodeMachineGrantAndRecovery.service.HouseMachineService;
import com.source.barcodeMachineStatus.entity.BarcodeMachineStatus;
import com.source.barcodeMachineStatus.vo.*;
import com.source.barcodeMachineStatus.mapper.BarcodeMachineStatusMapper;
import com.source.barcodeMachineStatus.service.BarcodeMachineStatusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.baseData.entity.BarcodeMachine;
import com.source.baseData.service.BarcodeMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fengq
 * @since 2021-02-27
 */
@Service
public class BarcodeMachineStatusServiceImpl extends ServiceImpl<BarcodeMachineStatusMapper, BarcodeMachineStatus> implements BarcodeMachineStatusService {

    @Autowired
    private BarcodeMachineStatusMapper barcodeMachineStatusMapper;

    @Autowired
    private HouseMachineService houseMachineService;

//    @Autowired
//    private ExecutorService executorService;


    @Override
    public List<StallsMessageVo> getStallsMessageVoById(String barcodeMachineId) {
        return barcodeMachineStatusMapper.getStallsMessageVoById(barcodeMachineId);
    }

    @Override
    public BarcodeMachineStatusVo getOneVo(String id) {
        return barcodeMachineStatusMapper.getOneVo(id);
    }

    @Override
    public IPage<BarcodeMachineStatusVo> getPageVo(Page<BarcodeMachineStatusVo> page) {
        return barcodeMachineStatusMapper.getPageVo(page);
    }

    @Override
    public List<UserNameVo> getAllUserNameAndId() {
        return barcodeMachineStatusMapper.getAllUserNameAndId();
    }

    @Override
    public IPage<BarcodeMachineStatusVo> getLikePageVo(Page<BarcodeMachineStatusVo> page, String keyword) {
        return barcodeMachineStatusMapper.getLikePageVo(page,keyword);
    }

    @Override
    public List<BarcodeMachineStatus> findListByStatus(String status) {
        QueryWrapper<BarcodeMachineStatus> wrapper = new QueryWrapper<>();
        wrapper.eq("state", status);

        List<BarcodeMachineStatus> list = list(wrapper);

        return list;
    }

    @Override
    public Map<String, Object> findStatusChart() {
        List<BarcodeMachineStatus> list0 = findListByStatus("0");
        List<BarcodeMachineStatus> list1 = findListByStatus("1");
        List<BarcodeMachineStatus> list2 = findListByStatus("2");
        List<BarcodeMachineStatus> list3 = findListByStatus("3");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("0",list0);
        map.put("1",list1);
        map.put("2",list2);
        map.put("3",list3);

        return map;
    }

    @Override
    public Map<String, Object> findStatusChart(String houseId) {


        Map<String, Object> map = new HashMap<String, Object>();

        List<BarcodeMachineStatus> list0 = new ArrayList<>();
        List<BarcodeMachineStatus> list1 = new ArrayList<>();
        List<BarcodeMachineStatus> list2 = new ArrayList<>();
        List<BarcodeMachineStatus> list3 = new ArrayList<>();

        map.put("0",list0);
        map.put("1",list1);
        map.put("2",list2);
        map.put("3",list3);


        QueryWrapper<HouseMachine> wrapper = new QueryWrapper<>();
        wrapper.eq("houseId",houseId);
        //根据屠宰场，查出屠宰场下的所有脚标机number
        List<HouseMachine> list = houseMachineService.list(wrapper);
        if(list.size() == 0){
            return map;
        }
        //获取number
        List<String> numbers = new ArrayList<>();
        for (HouseMachine houseMachine : list) {
            numbers.add(houseMachine.getMachineNumber());
        }

        //查询那些在监控表下的脚标机
        QueryWrapper<BarcodeMachineStatus> wrapper1 = new QueryWrapper<>();
        wrapper1.in("barcodeMachineNumber",numbers);
        List<BarcodeMachineStatus> statusList = list(wrapper1);

        for (BarcodeMachineStatus barcodeMachineStatus : statusList) {
            if("0".equals(barcodeMachineStatus.getState())) {
                list0.add(barcodeMachineStatus);
            }else if("1".equals(barcodeMachineStatus.getState())){
                list1.add(barcodeMachineStatus);
            }else if("2".equals(barcodeMachineStatus.getState())){
                list2.add(barcodeMachineStatus);
            }else if("3".equals(barcodeMachineStatus.getState())){
                list3.add(barcodeMachineStatus);
            }
        }


        return map;
    }

    @Override
    public List<BarcodeMachineStatusCountVo> selectCountBarcodeMachineByStatus(String houseId) {
        List<BarcodeMachineStatusCountVo> list=new ArrayList<>();
        list.add(new BarcodeMachineStatusCountVo("正常",barcodeMachineStatusMapper.selectCountBarcodeMachineByStatus("1",houseId)));
        list.add(new BarcodeMachineStatusCountVo("离线",barcodeMachineStatusMapper.selectCountBarcodeMachineByStatus("0",houseId)));
        list.add(new BarcodeMachineStatusCountVo("卡纸",barcodeMachineStatusMapper.selectCountBarcodeMachineByStatus("2",houseId)));
        list.add(new BarcodeMachineStatusCountVo("缺纸",barcodeMachineStatusMapper.selectCountBarcodeMachineByStatus("3",houseId)));
        return list;
    }

    @Override
    public List<BarcodeMachineAddressVo> selectBarcodeMachineAddressByHouseId(String houseId,String state) {
        return barcodeMachineStatusMapper.selectBarcodeMachineAddressByHouseId(houseId,state);
    }

    @Override
    public IPage<BarcodeCountChart> getBarcodeCountAndStallNameAndStatus(Page<BarcodeCountChart> page, String houseId, String status) {
        return barcodeMachineStatusMapper.selectBarcodeCountAndStallNameAndStatus(page,houseId,status);
    }

    @Override
    public List<BarcodeMachineAddressVo> getBarcodeMachineAddressByHouseIdAndKeyword(String houseId, String keyword) {
        return barcodeMachineStatusMapper.selectBarcodeMachineAddressByHouseIdAndKeyword(houseId,keyword);
    }


}
